﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Drawing;
using System.Windows.Forms;
using System.ComponentModel;

namespace Fcdbas.WinUI.Controls
{
    [Description("表示 Fcdbase 带SQL语法高的控件")]
    [ToolboxBitmap(typeof(RichTextBox))]
    [ToolboxItemFilter("System.Windows.Forms")]
    public class cRichTextBox: System.Windows.Forms.RichTextBox
    {
        public cRichTextBox()
        {
            keys = PLSQLKeyWorks.Split(' ');
        }
        [DllImport("user32")]
        private static extern int SendMessage(IntPtr hwnd, int wMsg, int wParam, IntPtr lParam);
        private string PLSQLKeyWorks = @"ADD ALL ALTER AND ANY ARRAY ARRAYLEN AS ASC ASSERT ASSIGN AT AVG BASE_TABLE BEGIN BETWEEN BINARY_INTEGER BY CASE CHAR CHAR_BASE CHECK COMMIT CRASH CREATE CURSOR DATE DECLARE DECIMAL DEFAULT DELETE DESC DISTINCT DO DROP ELSE ELSIF EXISTS EXIT FALSE FETCH FLOAT FOR FROM FUNCTION GROUP HAVING IF IN INSERT INTEGER INTERFACE INTERSECT INTO IS LIKE LIMITED LOOP MAX MAXEXTENTS MAXVALUE MIN MOD MODE NOT NOWAIT NULL NUMBER OF ON OR ORDER OTHERS OUT PROCEDURE RETURN REVERSE REVOKE ROLLBACK ROW objid ROWNUM ROWS SAVEPOINT SELECT SEPARATE SEQUENCE SET SQL SUM SYNONYM SYSDATE TABLE TABLES TASK THEN TO TRIGGER TRUE TYPE UID UNION UNIQUE UPDATE USE USER VALIDATE VALUES VARCHAR VARCHAR2 VARIANCE VIEW VIEWS WHEN WHENEVER WHERE WHILE WITH XOR DBMS_OUTPUT NEXT_DATE PUT REMOVE SUBMIT WHAT REPLACE RUN ABS ADD_MONTHS ASCII CEIL CHR CONCAT CONVERT COS COSH DECODE EXP FLOOR GREATEST HEXTORAW INITCAP INSTR LAST_DAY LENGTH LN LOG LOWER LPAD LTRIM MOD MONTHS_BETWEEN NEXT_DAY NEW_TIME NLS_LOWER NVL POWER RAWTOHEX REPLACE ROUND objidTOCHAR RPAD RTRIM SIGN SIN SINH SOUNDEX SQRT SUBSTR SUBSTRB TAN TANH TO_CHAR TO_DATE TO_LABEL TO_MULTI_BYTE TO_NUMBER TRUNC UPPER VSIZE BEFORE EACH FOR KEY WHEN WHERE WHILE";
        private string[] keys;
        //重写OnTextChanged方法：
        protected override void OnTextChanged(EventArgs e)
        {
            base.OnTextChanged(e);
            SendMessage(base.Handle, 0xB, 0, IntPtr.Zero);
            int sIndex = this.SelectionStart;
            this.SelectAll();
            this.SelectionColor = Color.Black;
            this.Select(sIndex, 0);
            //ChangeColor("static", Color.Blue);//调用改变文字颜色的方法
            //ChangeColor("void", Color.Blue);
            foreach (string s in keys)
            {
                ChangeColor(s, Color.Blue);
            }
            this.Select(sIndex, 0);
            this.SelectionColor = Color.Black;
            SendMessage(base.Handle, 0xB, 1, IntPtr.Zero);
            this.Refresh();
        }
        //定义改变文字颜色的私有方法：
        private void ChangeColor(string text, Color color)
        {
            int s = 0;
            while ((-1 + text.Length - 1) != (s = text.Length - 1 + this.Find(text, s, -1,  RichTextBoxFinds.WholeWord)))
            {
                this.SelectionColor = color;
            }
        }
    }
}
